package com.maserb.monitor.web.schoolForum;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolForum.CommentEntity;
import com.maserb.monitor.entity.schoolForum.CommentReplyEntity;
import com.maserb.monitor.entity.schoolForum.PostEntity;
import com.maserb.monitor.service.author.AuthorService;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.base.IToolService;
import com.maserb.monitor.service.jwEducation.JwOrgService;
import com.maserb.monitor.service.schoolForum.CommentReplyService;
import com.maserb.monitor.service.schoolForum.CommentService;
import com.maserb.monitor.service.schoolForum.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;
import sun.net.www.http.PosterOutputStream;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 学校论坛
 */
@Controller
@RequestMapping("/forum")
public class SchoolForumController {


    @Autowired
    PostService postService;

    @Autowired
    CommentService commentService;

    @Autowired
    CommentReplyService replyService;

    @Autowired
    IToolService toolService;

    @Autowired
    AuthorService authorService;

    @Autowired
    UserService userService;

    @Autowired
    JwOrgService orgService;

    //App调用
    @RequestMapping("/postList")
    @ResponseBody
    public PageResponseResult<PostEntity> postList(PageRequestParam page, String keyword,String orgId) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<PostEntity>>() {
            @Override
            public Page<PostEntity> invoke(PageRequest pageRequest) throws Exception {
                JwOrgEntity orgEntity = new JwOrgEntity();
                String schoolId = orgId;
                if(!StringUtils.isEmptyOrWhitespace(orgId)){
                    orgEntity = orgService.findOne(orgId);
                    //如果传入的是班级id
                    if(orgEntity.getTreeLevel()==2){
                        schoolId = orgEntity.getParent().getId();
                    }
                }else{
                    return null;
                }
                Page<PostEntity> page = postService.findAll(pageRequest, keyword,schoolId);
                for(PostEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                }
                return page;
            }
        });
    }

    /**
     * 发帖
     * @param entity
     * @return
     */
    @RequestMapping("/postSave")
    @ResponseBody
    public AjaxResult<Boolean> postSave(PostEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setIsTop("0");
                entity.setCreateTime(new Date());
                //如果是教师发帖，新增学校字段。
                if(!StringUtils.isEmptyOrWhitespace(entity.getSchoolId())){
                    JwOrgEntity orgEntity = orgService.findOne(entity.getSchoolId());
                    if(null!=orgEntity&&orgEntity.getTreeLevel()==1){
                        entity.setSchoolId(orgEntity.getId());
                        entity.setSchoolName(orgEntity.getName());
                    }else if(null!=orgEntity&&orgEntity.getTreeLevel()==2){
                        entity.setSchoolId(orgEntity.getParent().getId());
                        entity.setSchoolName(orgEntity.getParent().getName());
                    }
                }
                if(!StringUtils.isEmptyOrWhitespace(entity.getType())&&"2".equals(entity.getType())){
                    if(!StringUtils.isEmptyOrWhitespace(entity.getAuthorId())){
                        UserEntity teacher = userService.findOne(entity.getAuthorId());
                        if(null!=teacher){
                            entity.setSchoolId(teacher.getOrgId());
                            entity.setSchoolName(teacher.getOrgName());
                        }
                    }
                }
                postService.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/commentList")
    @ResponseBody
    public PageResponseResult<CommentEntity> commentList(PageRequestParam page, String postId) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<CommentEntity>>() {
            @Override
            public Page<CommentEntity> invoke(PageRequest pageRequest) throws Exception {
                Page<CommentEntity> page = commentService.findAll(pageRequest, postId);
                for(CommentEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                    List<CommentReplyEntity> replyList = replyService.findTopReplyList(entity);
                    entity.setReplyList(replyList);
                }
                return page;
            }
        });
    }

    /**
     * 评论
     * @param entity
     * @param postId
     * @return
     */
    @RequestMapping("/commentSave")
    @ResponseBody
    public AjaxResult<Boolean> commentSave(CommentEntity entity, String postId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                PostEntity post = postService.findOne(postId);
                entity.setCreateTime(new Date());
                entity.setPost(post);
                commentService.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/commentReplyList")
    @ResponseBody
    public PageResponseResult<CommentReplyEntity> commentReplyList(PageRequestParam page, String commentId) {
        Sort.Order codeOrder = new Sort.Order(Sort.Direction.DESC, "createTime");
        List<Sort.Order> orderList = new ArrayList<Sort.Order>();
        orderList.add(codeOrder);
        Sort sort = new Sort(orderList);
        return AjaxUtils.processPage(page,sort, new Func_T1<PageRequest, Page<CommentReplyEntity>>() {
            @Override
            public Page<CommentReplyEntity> invoke(PageRequest pageRequest) throws Exception {
                Page<CommentReplyEntity> page = replyService.findAll(pageRequest, commentId);
                for(CommentReplyEntity entity : page.getContent()){
                    entity.setFmtCreateTime(toolService.formatYYMMDDHHMMSS(entity.getCreateTime()));
                }
                return page;
            }
        });
    }

    /**
     * 评论
     * @param entity
     * @param commentId
     * @return
     */
    @RequestMapping("/commentReplySave")
    @ResponseBody
    public AjaxResult<Boolean> commentReplySave(final CommentReplyEntity entity, String commentId) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                CommentEntity comment = commentService.findOne(commentId);
                entity.setCreateTime(new Date());
                entity.setComment(comment);
                replyService.save(entity);
                return true;
            }
        });
    }
}
